"""
Copyright (c) 2024 [Baijiankang/DUT]
简易火车订票系统

联系信息: [jiankang_bai@163.com]
项目地址: [https://gitee.com/BJKgitee/simple-train-ticket-booking-system]

保留所有权利。
"""

import json
from tkinter import messagebox

def ticket_check(file_path, route, start, target):
    """
    查票，返回余票数
    :param file_path: JSON文件的路径
    :param route: 线路名
    :param start: 出发点位置
    :param target: 到达点位置
    return: 最少票数
    """
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)

    for item in data:
        index1 = -1
        index2 = -1
        if route in item['forward']:
            if start in item["stations"]:
                index1 = item["stations"].index(start)
            if target in item["stations"]:
                index2 = item["stations"].index(target)
            if index1 >= 0 and index2 >= 0:
                tickets = item['forward'][route][index1+1:index2+1]
                return min(tickets)
        if route in item['backward']:
            # print("backward!\n")
            if start in item["stations"]:
                index2 = item["stations"].index(start)
            if target in item["stations"]:
                index1 = item["stations"].index(target)
            if index1 >= 0 and index2 >= 0:
                tickets = item['backward'][route][index1:index2]
                return min(tickets)

    with open(file_path, 'w', encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False, indent=4)

    return None  # 如果没有找到票，返回 None


def ticket_sale(file_path, route, start, target):
    """
    买票
    :param file_path: JSON文件的路径
    :param route: 线路名
    :param start: 出发点位置
    :param target: 到达点位置
    """
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)

    for item in data:
        index1 = -1
        index2 = -1
        if route in item['forward']:
            if start in item["stations"]:
                index1 = item["stations"].index(start)
            if target in item["stations"]:
                index2 = item["stations"].index(target)
            if index1 >= 0 and index2 >= 0:
                if min(item['forward'][route][index1 + 1:index2 + 1]) > 0:
                    for i in range(index1+1, index2+1):
                        item['forward'][route][i] = item['forward'][route][i] - 1
                    break
                else:
                    messagebox.showinfo("警告", "票已售罄！")
                    break
        if route in item['backward']:
            if start in item["stations"]:
                index2 = item["stations"].index(start)
            if target in item["stations"]:
                index1 = item["stations"].index(target)
            if index1 >= 0 and index2 >= 0:
                if min(item['backward'][route][index1:index2]) > 0:
                    for i in range(index1, index2):
                        item['backward'][route][i] = item['backward'][route][i] - 1
                    break
                else:
                    messagebox.showinfo("警告", "票已售罄！")
                    break

    with open(file_path, 'w', encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False, indent=4)